window.onload = function() {
    var inputs = document.querySelectorAll('.body .con .bj_bd form table tr td input');
    console.log(inputs);
    var uname = inputs[0];
    var pass = inputs[1];
    var cpass = inputs[2];
    var username = inputs[3];
    console.log(username);
    var form = document.querySelector('.body .con .bj_bd form');
    form.addEventListener('submit', submit);
    function submit() {
        window.event.returnValue = false;
        var xhr = new XMLHttpRequest();
        xhr.open('post', 'http://localhost:8888/users/register');
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
        var params = `username=${uname.value}&password=${pass.value}&rpassword=${cpass.value}&nickname=${username.value}`
        xhr.send(params);
        xhr.onreadystatechange = function() {
            if(xhr.readyState === 4) {
                console.log(222);
                if(parseInt(xhr.status / 100) ===2) {
                    var res = xhr.responseText;
                    console.log(res);
                    res = JSON.parse(res);
                    var {code, message} = res;
                    if(code === 1) {
                        // alert(message);
                        location.href = './login.html';
                    } else {
                        alert(message);
                    }
                }
            }
        }
    }
}